<?php
session_start();
include_once $_SERVER['DOCUMENT_ROOT']."/math-videos/configure.inc.php";
//$_SESSION['user']=$_POST['username'];

//$_SESSION['user_id'] = $user_id;

//$_SESSION['admin'] = $admin_flag;

//echo $_SESSION['user_id']."<br>";
//echo "!!!!!!!!".$_SESSION['admin']."<br>";

if($_GET['page']!='')
{
	$cur_page = (int)$_GET['page'];
}
$limit=30;
if(isset($_REQUEST['id'] ) && $_REQUEST['id'] !='' && is_numeric($_REQUEST['id'])  && strtoupper($_REQUEST['del']) =='Y' )
{
  $id = $_REQUEST['id'];
  if($_SESSION['admin'])
	{
		
		$sqltext="delete 
		from logs where id=?";
		$params = array();	
		array_push($params,sql_escape($id));
		db_change_query($conn,$sqltext,$params);	
	}
	else
	{
		$sqltext="select institute_id from users  WHERE id=?";
		$params = array();
		
		array_push($params,sql_escape($_SESSION['user_id']));
		$result = db_select_query($conn2,$sqltext,$params);
	
		$institute_id='';
		while($row = db_fetch_array($result))
		{
			$institute_id = $row['institute_id'];
		}
		if($institute_id  !='')
		{
			$sqltext="delete 
			from logs where id=? and institute_id =?";
			$params = array();	
			array_push($params,sql_escape($id));
			array_push($params,sql_escape($institute_id));
			db_change_query($conn,$sqltext,$params);
		}
	}
}

$heading_area = $null;
$heading_area .= "<div style=\"font-family:Arial, Verdana, sans-serif; font-size : 12px;width:100%;padding-bottom:1px;\">";

$heading_area .= "<TABLE CELLPADDING=2 CELLSPACING=0 style=\"font-family:Arial, Verdana, sans-serif; font-size : 12px; color:white; background-color:#0099CC;width:100%;\"  >";
$heading_area .= "<tr >
										<td colspan=2  align=\"right\" style=\"padding-right:20px;font-weight:bold;font-size : 12px;\"> <a href='logout.php' style=\"color:#fff;text-decoration:none;\" >Logout</a>
										</td>
									</tr>";  
$heading_area .= "<tr >
										<td colspan=2  align=\"center\" width=\"100%\" style=\"font-size:26px;\"> Math Video Error Logs
										</td>
									</tr>";  
// put out blue row to hold action buttons (add, cancel, delete, etc.)
$heading_area .= "
		<tr>
			<td colspan=2 align=\"center\" ><a href='../admin/index.php' style=\"color:#fff;font-size : 12px;font-weight:bold;\"> < Admin Home Page</a>";
$heading_area .= "</td></tr>";

$heading_area .= "
	</TABLE></div>";
include "header.php";
echo $heading_area;
echo "<div style='width:100%;'>";
if($_SESSION['admin'])
{
	$sqltext="select id,error,
									to_char(added_date,'mm/dd/yyyy') as added_date, 
									user_id,institute_id									
									from logs
									order by id DESC";
	$params = array();
}
else
{
	$sqltext2="select institute_id from users  WHERE id=?";
  $params2 = array();
  $institute_id='';
  array_push($params2,sql_escape($_SESSION['user_id']));
  $result2 = db_select_query($conn2,$sqltext2,$params2);
 
  while($row2 = db_fetch_array($result2))
  {
	  $institute_id = $row2['institute_id'];
  }
  $sqltext="select id,
									error,
									to_char(added_date,'mm/dd/yyyy') as added_date, 
									user_id,
									institute_id									
					from logs
					WHERE institute_id =?
					order by id DESC";
	$params = array();	
	array_push($params,sql_escape($institute_id));
}
//echo "!!!!!!!!!!!!!".$sqltext."<br>";
//list all the asset information
	$result = db_select_query($conn,$sqltext,$params);
	unset($array_list);
	while($row = db_fetch_object($result))
	{
		$array_list[]=$row;
		
	}
	$name ='';
	$pre_institute_id='';
	$items_num=count($array_list);
	$limitvalue=0;
  if($items_num>$limit)	{
		$pagename = "error_log_report.php?";		
		if($cur_page !='')
		{
			$limitvalue = $limit*($cur_page-1);
		}		
	}
	$num_per_page=0;
	echo "<div align='right' style=\"padding-right:10px;\">";
				paginate($items_num,$limit,$cur_page,"#336699");
				echo "</div>";
	for($i=$limitvalue; $i<count($array_list);$i++)
	{
		
		foreach($array_list[$i] as $key => $value) 
		{
			$value = trim($value);
			$$key = $value;			
		}
		$search_num =0;
		$click_num =0;
	
		if($name =='')
		{
			$sqltext3="select full_name from institutes  WHERE id=?";
			$params3 = array();			
			array_push($params3,sql_escape($institute_id));
			$result3 = db_select_query($conn2,$sqltext3,$params3);
			while($row3 = db_fetch_array($result3))
			{
				$name = $row3['full_name'];
			}
			echo "<div align='left' style='padding-left:1%;font-size:14px;'><b>".$name."</b>:</div>";
			echo "<table  cellspacing='1' cellpadding='3' style='font-size:12px;background-color:#ccc;width:98%;' align=\"center\">";
			echo "<tr><th align='left'>Query Error</th><th align='left'>Added Date</th><th>&nbsp;</th></tr>";
			$pre_institute_id = $institute_id;
		}
		elseif($pre_institute_id != $institute_id)
		{
			$sqltext3="select full_name from institutes  WHERE id=?";
			$params3 = array();			
			array_push($params3,sql_escape($institute_id));
			$result3 = db_select_query($conn2,$sqltext3,$params3);
			while($row3 = db_fetch_array($result3))
			{
				$name = $row3['full_name'];
			}
			echo "</table ><br />";
			echo "<div align='left' style='padding-left:1%;font-size:14px;'><b>".$name."</b>:</div>";
			echo "<table cellspacing='1' cellpadding='3' style='font-size:12px;background-color:#ccc;width:98%;' align=\"center\">";
			echo "<tr><th align='left'>Query Error</th><th align='left'>Added Date</th><th>&nbsp;</th></tr>";
	
			$pre_institute_id = $institute_id;
		}
		echo "<tr bgcolor='#ffffff'><td>$error</a></td>";
		echo "<td>$added_date</td>";
		echo "<td align='center' style='width:120px;'><a href='../admin/error_log_report.php?id=$id&amp;del=Y&amp;' onclick=\"confirm('Do you really want to delete this log?')\">Delete</a></td></tr>";
		$num_per_page++;
		if($num_per_page >=$limit)
		{
			break;
		}
	}
echo "</table>";
echo "</div>";
include "footer.php"; 
	?>
